﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Controls_SpendExpOnRank : System.Web.UI.UserControl
{
    private HeresyManager.Rank rank;

  
    private Dictionary<HeresyManager.Rank.Buyable, TableRow> rowMap = new Dictionary<HeresyManager.Rank.Buyable, TableRow>();

    public HeresyManager.Character Character
    {
        get { return UserData.CurrentCharacter; }
    }

    public HeresyManager.Rank Rank
    {
        get { return rank; }
        set { rank = value; }
    }

    public Action<HeresyManager.Rank.Buyable, HeresyManager.Character> Accire { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = rank.Name;
        foreach (var b in Rank.Buyables)
        {
            Table1.Rows.Add(GetRowFomrBuyable(b));
        }
    }

    protected override void OnPreRender(EventArgs e)
    {

        base.OnPreRender(e);
        foreach (var b in Rank.Buyables)
        {
            //Table1.Rows.Add(GetRowFomrBuyable(b));
            rowMap[b].Enabled = b.IsPreconditionSattisfied(Character);
        }
    }

    private TableRow GetRowFomrBuyable(HeresyManager.Rank.Buyable b)
    {
        var row = new TableRow();

        var cellName = new TableCell();
        cellName.Text = b.ToString();
        row.Cells.Add(cellName);

        var cellCost = new TableCell();
        cellCost.Text = b.Cost.ToString();
        row.Cells.Add(cellCost);

        var cellButton = new TableCell();
        var button = new Button();
        cellButton.Controls.Add(button);
        row.Cells.Add(cellButton);

        button.Text = "Buy";

        button.Click += (x, y) =>
        {
            Accire(b, Character);
        };

        rowMap[b] = row;

        return row;
    }
}
